package com.mccken.practice.top100;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-28 19:42
 **/
public class ListNodeUtil {
	
	public static ListNode buildListNode(int[] arr) {
		if (arr.length == 0) {
			return null;
		}
		ListNode head = new ListNode(arr[0]);
		ListNode node = head;
		for (int i = 1; i < arr.length; i++) {
			node.next = new ListNode(arr[i]);
			node = node.next;
		}
		return head;
	}
	
	public static void printListNode(ListNode head){
		ListNode node = head;

		List<Integer> list = new ArrayList<>(); 
		
		while (node != null) {
			list.add(node.val);	
			node = node.next;
		}

		System.out.println(list);
		
	}
	
	public static Node buildRandomNode(int[][] arr) {
		if (arr.length == 0) {
			return null;
		}

		List<Node> nodeList = new ArrayList<>();
		Node head = new Node(arr[0][0]);
		nodeList.add(head);
		Node node = head;
		for (int i = 1; i < arr.length; i++) {
			node.next = new Node(arr[i][0]);
			node = node.next;
			nodeList.add(node);	
		}

		for (int i = 0; i < arr.length; i++) {
			nodeList.get(i).random = nodeList.get(arr[i][1]);	
		}
		
		return nodeList.get(0);
	}

	public static void main(String[] args) {
		// [[7,null],[13,0],[11,4],[10,2],[1,0]]
//		buildRandomNode(new int[][]{{7, Integer.parseInt(null)},{13,0},{11,4},{10,2},{1,0}});

		JSONArray objects = JSONUtil.parseArray("[[7,null],[13,0],[11,4],[10,2],[1,0]]", false);
		System.out.println(objects);

	}
	
	
}
